package ess.hardware;

public class ArrivalSensor {
	public static int DEPLOY_POSITION_UP = 1;
	public static int DEPLOY_POSITION_DOWN = 2;
	
	public static int ELEVATOR_DETECTED = 1;
	public static int ELEVATOR_LOST = 2;
	
	int elevatorId;
	int deployedFloor;
	int deployedPosition;
	Elevator elevator;
	int state;
	ArrivalSensorEventListener listener = null;
	
	public ArrivalSensor(Elevator elevator, int floor, int position){
		this.elevator = elevator;
		this.deployedFloor = floor;
		this.deployedPosition = position;
	}
	
	
	public int getDeployedFloor() {
		return deployedFloor;
	}


	public int getDeployedPosition() {
		return deployedPosition;
	}


	public void addArrivalSensorEventListener(ArrivalSensorEventListener l){
		listener = l;
	}
	
	public void NotifyDetected(){
		listener.notifyArrivalSensorStateChanged(new ArrivalSensorEvent(elevator.getId(), deployedFloor, deployedPosition, ELEVATOR_DETECTED));
	}
/*
	@Override
	public void run() {

		float normalizedPosition = elevator.getNormalizedPosition();
		if(normalizedPosition == (float)deployedFloor){
			state = ELEVATOR_DETECTED;
		}
		else
		{
			state = ELEVATOR_LOST;
		}

		while(true){
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			normalizedPosition = elevator.getNormalizedPosition();
			int changedState = -1;
			
			if(deployedPosition == DEPLOY_POSITION_UP){
				if(normalizedPosition >= (float)deployedFloor && normalizedPosition < (float)deployedFloor+1.0f){
					changedState = ELEVATOR_DETECTED;
				}
				else{
					changedState = ELEVATOR_LOST;
				}
			}
			else{
				if(normalizedPosition <= (float)deployedFloor && normalizedPosition > (float)deployedFloor-1.0f){
					changedState = ELEVATOR_DETECTED;
				}
				else{
					changedState = ELEVATOR_LOST;
				}
				
			}
			
			if(state != changedState && listener != null){
				state = changedState;
				System.out.println("ArrivalSensor.notifyArrivalSensorStateChanged");
				listener.notifyArrivalSensorStateChanged(new ArrivalSensorEvent(elevator.getId(), deployedFloor, deployedPosition, state));
			}
		}
		
	}
	*/
}
